package com.mkb.controller;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.mkb.common.ModuleURL;
import com.mkb.common.URL;
import com.mkb.entity.AreaDTO;
import com.zz.common.result.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.mkb.service.AreaService;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(ModuleURL.AREA)
public class AreaController {
	@Autowired
    private AreaService areaService;
 
    @RequestMapping(value = URL.OP_LIST, method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest request) throws Exception{
        return new ModelAndView("page/area/list");
    }
 
    @RequestMapping(value = URL.OP_LIST, method = RequestMethod.POST)
    public ModelAndView tableList(Model model, @RequestBody AreaDTO areaDTO) throws Exception{
        PageInfo<AreaDTO> response = areaService.queryPage(areaDTO);
        model.addAttribute("pageInfo", response);
        return new ModelAndView("page/area/table_list");
    }
 
    @RequestMapping(value = URL.OP_CREATE, method = RequestMethod.GET)
    public ModelAndView createPage(Model model, AreaDTO areaDTO) throws Exception{
        return new ModelAndView("page/area/create");
    }
 
    @RequestMapping(value = URL.OP_CREATE, method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo createSave(Model model, @RequestBody AreaDTO areaDTO) throws Exception{
        areaService.create(areaDTO);
        return  ResultInfo.ok();
    }
 
    @RequestMapping(value = URL.OP_DELETE, method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo del(@RequestParam(required = false, value = "idList[]") List<Long> idList) throws Exception{
        for (Long id : idList){
            areaService.deleteById(id);
        }
        return ResultInfo.ok();
    }
 
    @RequestMapping(value = URL.OP_MODIFY, method = RequestMethod.GET)
    public ModelAndView modifyPage(Model model, Long id) throws Exception{
        model.addAttribute("area", areaService.getById(id));
        return new ModelAndView("page/area/modify");
    }
 
    @RequestMapping(value = URL.OP_MODIFY, method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo modifySave(Model model, @RequestBody AreaDTO areaDTO) throws Exception{
        areaService.update(areaDTO);
        return  ResultInfo.ok();
    }
}
